﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
// 加载本地图片
public class ExternalPic : MonoBehaviour
{
    public Image image;
    public RawImage rawImage;
    
    private readonly string _url = "C://Users/Administrator/Pictures/picTest.jpg";

    private void Start()
    {
        // TestLoadPicByIO();
        TestLoadPicByWWW();
    }

    private void TestLoadPicByWWW()
    {
        StartCoroutine(LoadTexture2DByWWW(_url, rawImage));  
        StartCoroutine(LoadSpriteByWWW(_url, image));                            
    }

    private void TestLoadPicByIO()
    {
        var texture = LoadTexture2DByIO(_url);
        if (texture != null) rawImage.texture = texture;
        var sprite = LoadSpriteByIO(_url);
        if (sprite != null) image.sprite = sprite;
    }

    #region IO 加载外部图片

    /// <summary>                                                                                           
    /// 使用 IO 流加载图片，并返回。                                                                                    
    /// </summary>                                                                                          
    /// <param name="_url">图片地址</param>                                                                     
    /// <returns></returns>                                                                                 
    public static Texture2D LoadTexture2DByIO(string _url)
    {
        //创建文件读取流                                                                                           
        FileStream _fileStream = new FileStream(_url, FileMode.Open, FileAccess.Read);
        _fileStream.Seek(0, SeekOrigin.Begin);
        //创建文件长度缓冲区                                                                                         
        byte[] _bytes = new byte[_fileStream.Length];
        _fileStream.Read(_bytes, 0, (int)_fileStream.Length);
        _fileStream.Close();
        _fileStream.Dispose();
        //创建Texture                                                                                         
        Texture2D _texture2D = new Texture2D(1, 1);
        _texture2D.LoadImage(_bytes);
        return _texture2D;
    }

    /// <summary>                                                                                           
    /// 使用 IO 流加载图片，并将图片转换成 Sprite 类型返回                                                                     
    /// </summary>                                                                                          
    /// <param name="_url">图片地址</param>                                                                     
    /// <returns></returns>                                                                                 
    public static Sprite LoadSpriteByIO(string _url)
    {
        Texture2D _texture2D = LoadTexture2DByIO(_url);
        Sprite _sprite = Sprite.Create(_texture2D, new Rect(0, 0, _texture2D.width, _texture2D.height),
            new Vector2(0.5f, 0.5f));
        return _sprite;
    }

    #endregion

    #region UnityWebRequest 加载外部图片
    /// <summary>
    /// 加载texture2D
    /// </summary>
    /// <param name="_url">资源地址</param>
    /// <param name="_rawImage">RawImage组件</param>
    /// <returns></returns>
    public static IEnumerator LoadTexture2DByWWW(string _url, RawImage _rawImage)
    {
        UnityWebRequest request = new UnityWebRequest(_url);
        DownloadHandlerTexture downloadHandlerTexture = new DownloadHandlerTexture(true);
        request.downloadHandler = downloadHandlerTexture;
        yield return request.SendWebRequest();
        if (string.IsNullOrEmpty(request.error))
        {
            _rawImage.texture = downloadHandlerTexture.texture;
            request.Dispose();
        }
        else
        {
            Debug.LogError(request.error);
        }
    }
   /// <summary>
   /// 加载sprite
   /// </summary>
   /// <param name="_url">资源路径</param>
   /// <param name="_image">Image组件</param>
   /// <returns></returns>
    public static IEnumerator LoadSpriteByWWW(string _url, Image _image)
    {
        UnityWebRequest request = new UnityWebRequest(_url);
        DownloadHandlerTexture downloadHandlerTexture = new DownloadHandlerTexture(true);
        request.downloadHandler = downloadHandlerTexture;
        yield return request.SendWebRequest();
        if (string.IsNullOrEmpty(request.error))
        {
            var texture = downloadHandlerTexture.texture;
            _image.sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
            request.Dispose();
        }
        else
        {
            Debug.LogError(request.error);
        }
    }

    #endregion
}